package com.listview;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baidu.location.demo.R;
import com.bumptech.glide.Glide;
import com.entity.Response;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.util.DialogUtil;
import com.util.HttpUtils;

import java.util.List;

public class FriendrequestAdapter extends ArrayAdapter {
    List<FriendrequestList> data;
    private final int resourceId;
    private Activity mActivity;
    public FriendrequestAdapter(Context mContext, int textViewResourceId, List<FriendrequestList> mData,Activity activity){
        super(mContext,textViewResourceId,mData);
        resourceId=textViewResourceId;
        data=mData;
        mActivity=activity;
    }

    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    public View getView(int i, @Nullable View convertView, @NonNull ViewGroup parent) {
        viewData view;
        if(convertView==null){
            convertView= LayoutInflater.from(getContext()).inflate(resourceId, null);
            view=new viewData();
            view.friendrequestImage=convertView.findViewById(R.id.friendrequestImage);
            view.friendrequestName=convertView.findViewById(R.id.friendrequestName);
            view.friendrequestRemark=convertView.findViewById(R.id.friendrequestRemark);
            view.friendrequestRefuse=convertView.findViewById(R.id.friendrequestRefuse);
            view.friendrequestConsent=convertView.findViewById(R.id.friendrequestConsent);
            view.friendrequestStatue=convertView.findViewById(R.id.friendrequestStatue);
            convertView.setTag(view);
        }else {
            view=(viewData) convertView.getTag();
        }
        Glide.with(getContext()).load(data.get(i).getImage()).into(view.friendrequestImage);
        view.friendrequestRemark.setText(data.get(i).getRemark());
        view.friendrequestName.setText(data.get(i).getName());
        if(data.get(i).getFriendStatus()==0){
            view.friendrequestStatue.setVisibility(View.GONE);
            view.friendrequestConsent.setVisibility(View.VISIBLE);
            view.friendrequestRefuse.setVisibility(View.VISIBLE);
            view.friendrequestRefuse.setOnClickListener(view1 -> {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        Response<Boolean> json= JSONObject.parseObject(HttpUtils.Put("Friends/ApplicationStatus?id="+data.get(i).getId()+"&statue=2",""),new TypeReference<Response<Boolean>>(){});
                        if(json.getStatusCode()==200){
                            DialogUtil.CreateDoalog(mActivity,"拒绝成功",0, QMUITipDialog.Builder.ICON_TYPE_SUCCESS);
                            mActivity.runOnUiThread(() -> {
                                view.friendrequestStatue.setVisibility(View.VISIBLE);
                                view.friendrequestConsent.setVisibility(View.GONE);
                                view.friendrequestRefuse.setVisibility(View.GONE);
                                view.friendrequestStatue.setText("拒绝成功");
                            });
                            return;
                        }
                        DialogUtil.CreateDoalog(mActivity,json.getMessage(),0, QMUITipDialog.Builder.ICON_TYPE_FAIL);
                    }
                }.start();
            });
            view.friendrequestConsent.setOnClickListener(view12 -> {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        Response<Boolean> json= JSONObject.parseObject(HttpUtils.Put("Friends/ApplicationStatus?id="+data.get(i).getId()+"&statue=1",""),new TypeReference<Response<Boolean>>(){});
                        if(json.getStatusCode()==200){
                            DialogUtil.CreateDoalog(mActivity,"拒绝成功",0, QMUITipDialog.Builder.ICON_TYPE_SUCCESS);
                            mActivity.runOnUiThread(() -> {
                                view.friendrequestStatue.setVisibility(View.VISIBLE);
                                view.friendrequestConsent.setVisibility(View.GONE);
                                view.friendrequestRefuse.setVisibility(View.GONE);
                                view.friendrequestStatue.setText("拒绝成功");
                            });
                            return;
                        }
                        DialogUtil.CreateDoalog(mActivity,json.getMessage(),0, QMUITipDialog.Builder.ICON_TYPE_FAIL);
                    }
                }.start();
            });
        }else {
            view.friendrequestStatue.setText(data.get(i).getFriendStatus()==1?"已同意":"已拒绝");
        }
        return convertView;
    }
    class viewData{
        ImageView friendrequestImage;
        TextView friendrequestName;
        TextView friendrequestRemark;
        Button friendrequestRefuse;
        TextView friendrequestStatue;
        Button friendrequestConsent;
    }
}
